package com.ruoyi.system.course.service.impl;

import java.util.List;

import com.ruoyi.system.statis.domain.ServiceStatisCourse;
import com.ruoyi.system.statis.mapper.ServiceStatisCourseMapper;
import com.ruoyi.system.statis.service.IServiceStatisCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.course.mapper.ServiceCourseInfoMapper;
import com.ruoyi.system.course.domain.ServiceCourseInfo;
import com.ruoyi.system.course.service.IServiceCourseInfoService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

/**
 * 课程信息Service业务层处理
 *
 * @author Administrator
 * @date 2021-03-06
 */
@Service
public class ServiceCourseInfoServiceImpl implements IServiceCourseInfoService {
    @Autowired
    private ServiceCourseInfoMapper serviceCourseInfoMapper;

    @Autowired
    private IServiceStatisCourseService iServiceStatisCourseService;

    /**
     * 查询课程信息
     *
     * @param id 课程信息ID
     * @return 课程信息
     */
    @Override
    public ServiceCourseInfo selectServiceCourseInfoById(Long id) {
        return serviceCourseInfoMapper.selectServiceCourseInfoById(id);
    }

    /**
     * 查询课程信息列表
     *
     * @param serviceCourseInfo 课程信息
     * @return 课程信息
     */
    @Override
    public List<ServiceCourseInfo> selectServiceCourseInfoList(ServiceCourseInfo serviceCourseInfo) {
        return serviceCourseInfoMapper.selectServiceCourseInfoList(serviceCourseInfo);
    }

    /**
     * 新增课程信息
     *
     * @param serviceCourseInfo 课程信息
     * @return 结果
     */
    @Override
    public int insertServiceCourseInfo(ServiceCourseInfo serviceCourseInfo) {

        //新增课程信息时 课程统计如果存在记录 计数+1 否则新建记录 计数+1
        int info = serviceCourseInfoMapper.insertServiceCourseInfo(serviceCourseInfo);
        if (!ObjectUtils.isEmpty(serviceCourseInfo)) {
            //根据名称查 如果有 +1
            ServiceStatisCourse course = new ServiceStatisCourse();
            course.setCourseName(serviceCourseInfo.getCourseName());
            List<ServiceStatisCourse> list = iServiceStatisCourseService.selectServiceStatisCourseList(course);
            if (!CollectionUtils.isEmpty(list)) {
                course.setCourseNum(course.getCourseNum() + 1);
                iServiceStatisCourseService.updateServiceStatisCourse(course);
            } else {
                ServiceStatisCourse courseInsert = new ServiceStatisCourse();
                courseInsert.setCourseName(serviceCourseInfo.getCourseName());
                courseInsert.setCourseNum(1L);
                iServiceStatisCourseService.insertServiceStatisCourse(courseInsert);
            }

        }

        return info;
    }

    /**
     * 修改课程信息
     *
     * @param serviceCourseInfo 课程信息
     * @return 结果
     */
    @Override
    public int updateServiceCourseInfo(ServiceCourseInfo serviceCourseInfo) {
        return serviceCourseInfoMapper.updateServiceCourseInfo(serviceCourseInfo);
    }

    /**
     * 删除课程信息对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteServiceCourseInfoByIds(String ids) {
        return serviceCourseInfoMapper.deleteServiceCourseInfoByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除课程信息信息
     *
     * @param id 课程信息ID
     * @return 结果
     */
    @Override
    public int deleteServiceCourseInfoById(Long id) {
        return serviceCourseInfoMapper.deleteServiceCourseInfoById(id);
    }
}
